package com.gxa.finance.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.common.commons.R;
import com.gxa.finance.pojo.Bill;
import com.gxa.finance.service.PayService;
import com.gxa.order.feign.ServiceOrderApi;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/finance/pay")
public class PayController {

    @Autowired
    PayService payService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    BillController billController;

    @Autowired
    ServiceOrderApi serviceOrderApi;


    //服务订单限时支付
    @PostMapping(value = "/cleanOrderLimit")
    public void cleanOrderLimit(String cleanOrderNumber,Integer orderType ){
        Map<String,Object> map = new HashMap<>();
        map.put("number",cleanOrderNumber);
        map.put("orderType",orderType);
        rabbitTemplate.convertAndSend("delay_exchange_name","",map);
    }


    //住宿订单限时支付
    @PostMapping(value = "/hotelOrderLimit")
    public void hotelOrderLimit(String orderNumber,Integer orderType){
        Map<String,Object> map = new HashMap<>();
        map.put("number",orderNumber);
        map.put("orderType",orderType);
        rabbitTemplate.convertAndSend("delay_exchange_name","",map);
    }


    //分类支付
    @PostMapping(value = "/payClassification")
    public R payClassification(@RequestBody Bill bill) throws JsonProcessingException {

        //获取订单类型
        int flag = "服务".equals(bill.getpType()) ? 2 : 1;
        //判断订单状态
        if(flag == 1){
            //根据订单编号查询对应的订单id
            int id = (Integer)serviceOrderApi.findid(bill.getOrderId()).getData();
            //根据订单id查询订单状态
            int status = (Integer)serviceOrderApi.findOrderStatu(id).getData();
            //判断订单是否取消
            if(status == 9){
                R r = new R();
                r.setCode(200);
                r.setMsg("支付超时，请重新下单");
                return r;
            }
        }
        else {
            //根据订单编号查询对应的订单id
            int id = (Integer)serviceOrderApi.findOrderIdByNumber(bill.getOrderId()).getData();
            //根据订单id查询订单状态
            Object data = serviceOrderApi.findCleanOrder5(id).getData();
            //利用springboot自带的jackson将object转成map
            ObjectMapper mapper = new ObjectMapper();
            Map map01 = mapper.readValue(mapper.writeValueAsString(data), Map.class);
            int status = Integer.parseInt(map01.get("status").toString());
            //判断订单是否取消
            if(status == 4){
                R r = new R();
                r.setCode(200);
                r.setMsg("支付超时，请重新下单");
                return r;
            }
        }

        //调用接口将初始订单入库
        billController.addBill(bill);

        //酒店订单支付
        if("酒店".equals(bill.getpType())){
            return payService.tavernPay(bill);
        }
        //民宿订单支付
        else if("民宿".equals(bill.getpType())){
            return null;
        }
        //服务订单支付
        else{
            return bill.getPayment().equals("线下支付")? payService.serviceOfflinePay(bill) : payService.servicePay(bill); //线下或线上支付
        }
    }
}
